from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, ForeignKey, Enum, JSON, Float
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.database import Base
import enum


class DataAssetType(str, enum.Enum):
    """数据资产类型枚举"""
    DATABASE = "database"
    TABLE = "table"
    VIEW = "view"
    COLUMN = "column"
    API = "api"
    FILE = "file"
    STREAM = "stream"


class DataAsset(Base):
    """数据资产模型"""
    __tablename__ = "data_assets"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(200), nullable=False, index=True)
    display_name = Column(String(200), nullable=True)
    description = Column(Text, nullable=True)
    asset_type = Column(Enum(DataAssetType), nullable=False)
    
    # 层级关系
    parent_id = Column(Integer, ForeignKey("data_assets.id"), nullable=True)
    level = Column(Integer, default=0)  # 层级深度
    full_path = Column(String(500), nullable=False, index=True)  # 完整路径
    
    # 资产属性
    datasource_id = Column(Integer, ForeignKey("datasources.id"), nullable=True)
    schema_info = Column(JSON, nullable=True)  # 结构信息
    tags = Column(JSON, nullable=True)  # 标签列表
    
    # 元数据
    owner = Column(String(100), nullable=True)
    steward = Column(String(100), nullable=True)  # 数据管家
    business_glossary = Column(Text, nullable=True)  # 业务词汇
    technical_metadata = Column(JSON, nullable=True)  # 技术元数据
    
    # 统计信息
    row_count = Column(Integer, nullable=True)
    data_size = Column(String(50), nullable=True)
    last_updated = Column(DateTime(timezone=True), nullable=True)
    update_frequency = Column(String(50), nullable=True)  # 更新频率
    
    # 质量评分
    quality_score = Column(Float, nullable=True)  # 数据质量评分 0-100
    popularity_score = Column(Float, nullable=True)  # 热度评分 0-100
    
    # 创建信息
    creator_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 关系
    parent = relationship("DataAsset", remote_side=[id])
    children = relationship("DataAsset")
    datasource = relationship("DataSource")
    creator = relationship("User")


class DataCatalog(Base):
    """数据目录模型"""
    __tablename__ = "data_catalogs"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, index=True)
    description = Column(Text, nullable=True)
    
    # 目录配置
    parent_id = Column(Integer, ForeignKey("data_catalogs.id"), nullable=True)
    icon = Column(String(50), nullable=True)
    sort_order = Column(Integer, default=0)
    
    # 权限配置
    is_public = Column(Boolean, default=True)
    allowed_roles = Column(JSON, nullable=True)
    
    # 创建信息
    creator_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 关系
    parent = relationship("DataCatalog", remote_side=[id])
    children = relationship("DataCatalog")
    creator = relationship("User")


class DataDictionary(Base):
    """数据字典模型"""
    __tablename__ = "data_dictionaries"

    id = Column(Integer, primary_key=True, index=True)
    term = Column(String(100), nullable=False, index=True)
    definition = Column(Text, nullable=False)
    
    # 分类信息
    category = Column(String(50), nullable=True)
    domain = Column(String(50), nullable=True)  # 业务域
    
    # 详细信息
    aliases = Column(JSON, nullable=True)  # 别名列表
    related_terms = Column(JSON, nullable=True)  # 相关术语
    examples = Column(Text, nullable=True)
    business_rules = Column(Text, nullable=True)  # 业务规则
    
    # 状态信息
    status = Column(String(20), default="active")  # active, deprecated, draft
    version = Column(String(20), default="1.0")
    
    # 创建信息
    creator_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    reviewer_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    reviewed_at = Column(DateTime(timezone=True), nullable=True)
    
    # 关系
    creator = relationship("User", foreign_keys=[creator_id])
    reviewer = relationship("User", foreign_keys=[reviewer_id]) 